/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

/**
 *
 * @author sheked
 */
public class CategoriaDB {
    private QueryRunner run;
    private MapListHandler mlh;
    private MapHandler mh;

    public CategoriaDB() throws NamingException, SQLException {
            Context ctx = new InitialContext();
            DataSource odsconn = (DataSource)ctx.lookup("jdbc/volansbd");
            run = new QueryRunner(odsconn);
            mlh = new MapListHandler();
            mh = new MapHandler();
    }

    public List<Map<String, Object>> consultaCategories() throws SQLException {
        return run.query("select * from categoria order by nom_cat asc", mlh);
    }

    public List<Map<String, Object>> consultaSubcategories(String idCat) throws SQLException {
        return run.query("select * from subcategoria where id_cat=? order by nom_subcat asc", mlh, idCat);
    }

    public Map<String, Object> consultaSubcategoria(String id) throws SQLException {
        return run.query("select * from subcategoria where id_subcat=?", mh, id);
    }

    public Map<String, Object> consultaCategoria(String id) throws SQLException {
        return run.query("select * from categoria where id_cat=?", mh, id);
    }
}
